<?php
/* User controller 
** thaodx@gmail.com
*/
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {
	var $_tableName = 'users';
	
	public function index(){
	
	}
	public function logout(){
		$this->load->helper('url');
		session_destroy();
		$data['sso'] = $_SESSION['sso'];
		$this->layout->view('user_logout', $data);
	}
	public function login(){
		$this->layout->title('User login'); // Set page title
		$this->layout->desc('user login'); // Set meta description
		$this->layout->view('user_login');// Render view and layout
	}
	public function fbauth(){
		$token = $_POST['token'];
		$this->load->helper('social');
		$this->load->model('User_model', 'user');
		$graphApiUser = 'https://graph.facebook.com/me?access_token='.$token;
		$response = cURL($graphApiUser);
		if($response){
			$userData = json_decode($response, true);
			if(!empty($userData) && $userData['id']){
				if($userData['gender'] == 'male' || $userData['gender'] == 'female' ){
					$gender = strtoupper($userData['gender']);
				}
				else{
					$gender = '';
				}
				$params = array(
					'user_social_id'	=>	$userData['id'],
					'user_email'		=>	$userData['email'],
					'user_fullname'		=>	$userData['name'],
					'user_role'			=>	'member',
					'user_sso'			=>	'FACEBOOK',
					'user_gender'		=>	$gender,
					'user_sso_rawdata'	=>	json_encode($userData)
				);
				if($userInfo = $this->user->saveSocialData($params)){
					$_SESSION['user'] = array(
						'id'	=>	$userInfo[0]['user_id'],
						'name'	=>	$userInfo[0]['user_fullname'],
						);
					$_SESSION['sso'] = 'FACEBOOK';
					$_SESSION['token'] = $token;
					echo '1';
				}
				else{
					echo '0';
				}
			}
		}
		die;
	}
	public function gpluscb(){
		die;
	}
	public function gplusauth(){
		$this->load->helper('social');
		$this->load->model('User_model', 'user');
		$token = $_POST['token'];
		$graphApiUser = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='.$token;
		$response = cURL($graphApiUser);
		if($response){
			$userData = json_decode($response, true);
			if(!empty($userData) && $userData['id']){
				if($userData['gender'] == 'male' || $userData['gender'] == 'female' ){
					$gender = strtoupper($userData['gender']);
				}
				else{
					$gender = '';
				}
				$params = array(
					'user_social_id'	=>	$userData['id'],
					'user_email'		=>	$userData['email'],
					'user_fullname'		=>	$userData['name'],
					'user_role'			=>	'member',
					'user_sso'			=>	'GOOGLE',
					'user_gender'		=>	$gender,
					'user_sso_rawdata'	=>	json_encode($userData)
				);
				if($userInfo = $this->user->saveSocialData($params)){
					$_SESSION['user'] = array(
						'id'	=>	$userInfo[0]['user_id'],
						'name'	=>	$userInfo[0]['user_fullname'],
						);
					$_SESSION['sso'] = 'GOOGLE';
					$_SESSION['token'] = $token;
					echo '1';
				}
				else{
					echo '0';
				}
			}
		}
		die;
	}
}